﻿namespace KeppelLand.Services
{
    using KeppelLand.Entities;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Activation;
    using System.ServiceModel.Web;
    using System.Text;


    [ServiceContract(Namespace = "KeppelLand.Services")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TestService
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public KeppelLand.Entities.RejectionDetailView DoWork(string id)
        {
            KeppelLand.Entities.RejectionDetailView view = null;

            // Add your operation implementation here
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EOCS"].ConnectionString))
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM Claim WHERE Id = @Id;";
                cmd.Parameters.AddWithValue("@ID", id);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);

                if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
                {
                    view = ds.Tables[0].AsEnumerable().Select(r => new RejectionDetailView
                    {
                        RejectorId = KeppelLand.Helpers.TranslationHelper.GetNameLegalByEmployeeMaster(r.Field<string>("RejectorId")),
                        RejectReason = r.Field<string>("RejectReason"),
                        RejectDate = r.Field<DateTime?>("RejectDate").HasValue ? r.Field<DateTime?>("RejectDate").Value.ToString("yyyy-MM-dd") : "N/A"
                    }).FirstOrDefault();
                }
            }

            return view;
        }

        // Add more operations here and mark them with [OperationContract]

        //[OperationContract]
        //[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Json,
        //            UriTemplate = "GetSearchResults")]
        //System.Xml.Linq.XElement GetSearchResults(inputParameters parameters);

        //[OperationContract]
        //[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "getFile")]
        //System.IO.Stream GetFile(DocInfo info);

    }
}